/*
 * Copyright 2008 Federal Emergency Management Agency
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); 
 * you may not use this file except in compliance with the License. 
 * You may obtain a copy of the License at 
 * 
 *    http://www.apache.org/licenses/LICENSE-2.0 
 *    
 * Unless required by applicable law or agreed to in writing, software 
 * distributed under the License is distributed on an "AS IS" BASIS, 
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and 
 * limitations under the License. 
 *  
 */
package gov.fema.hazus.bdc.client.hazards.flood.hazard;


import gov.fema.hazus.bdc.shared.entity.FloodHazard;

import java.util.Date;

import com.google.gwt.core.client.GWT;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;

public class FloodHazardTextBoxKeyListener implements FloodHazardTextBoxKeyListenerInterface {

	private FloodHazard floodHazard;
	
	public FloodHazardTextBoxKeyListener(FloodHazard floodHazard)
	{
		this.floodHazard = floodHazard;
	}
	
	public void onFocus(Widget arg0) {
		// do nothing
	}

	public void onLostFocus(Widget arg0) {

		TextBox textBox = (TextBox) arg0;
		String boxName = textBox.getName();
		
		String value = textBox.getText();
		
		Date dateValue = new Date();
		Integer intValue = 0;
		
		// date
		try {
			DateTimeFormat f = DateTimeFormat.getFormat("MM/dd/yyyy");
			dateValue = f.parse(value);
		} catch (Exception e) {
			GWT.log("Can't create date from string=" + value, null);
		}
		
		// integer
		try {
			intValue = Integer.valueOf(value);
		} catch (Exception e) {
			GWT.log("Can't create Integer from string=" + value, null);
		}
		//
		
		GWT.log("text box name=" + boxName, null);
		
		if( boxName.equals(FloodHazard.FIRMPANEL) )
		{
			GWT.log( FloodHazard.FIRMPANEL + ": " + value, null);
			this.floodHazard.setFIRMPanel(value);
		} else if ( boxName.equals( FloodHazard.FIRMCOMMUNITY ) ) {
			GWT.log( FloodHazard.FIRMCOMMUNITY + ": " + value, null);
			this.floodHazard.setFIRMCommunity(value);			
		} else if ( boxName.equals( FloodHazard.FIRMEFFECTIVE ) ) {
			GWT.log( FloodHazard.FIRMEFFECTIVE + ": " + dateValue.toString(), null);
			this.floodHazard.setFIRMEffectiveDate(dateValue);
		} else if ( boxName.equals( FloodHazard.FIRMREVISED ) ) {
			GWT.log( FloodHazard.FIRMREVISED + ": " + dateValue.toString(), null);
			this.floodHazard.setFIRMRevisedDate(dateValue);		
		} else if ( boxName.equals( FloodHazard.BASEFLOODELEVATION ) ) {
			GWT.log( FloodHazard.BASEFLOODELEVATION + ": " + value, null);
			this.floodHazard.setBaseFloodElevation(intValue);	
		} else if ( boxName.equals( FloodHazard.OTHERVERTICALDATUM ) ) {
			GWT.log( FloodHazard.OTHERVERTICALDATUM + ": " + value, null);
			this.floodHazard.setOtherVerticalDatum(value);		
		} else if ( boxName.equals( FloodHazard.BASEFLOODELEVATIONPLUS ) ) {
			GWT.log( FloodHazard.BASEFLOODELEVATIONPLUS + ": " + value, null);
			this.floodHazard.setBaseFloodElevationPlus(intValue);		
		} else if ( boxName.equals( FloodHazard.LOWESTGRADEELEVATION ) ) {
			GWT.log( FloodHazard.LOWESTGRADEELEVATION + ": " + value, null);
			this.floodHazard.setLowestGradeElevation(intValue);	
		} else {
			// do nothing
		}	
		
	}

	public void updateFloodHazard(FloodHazard floodHazard) {
		this.floodHazard = floodHazard;
	}

}
